package edu.thu.thss.yxy.util;

import java.util.ArrayList;
import java.util.List;

import javax.media.format.AudioFormat;
import javax.media.format.VideoFormat;
import javax.sdp.SdpConstants;

/**
 * Implements static utility methods used by media classes.
 * 
 * @author Andy Gao
 * 
 */
public class MediaUtil {
	/**
	 * Returned by {@link #sdpToJmfEncoding(String)} if it does not know the
	 * given encoding.
	 */
	public static final int UNKNOWN_ENCODING = -1;

	/**
	 * Returns the String encoding, as specified in AudioFormat and VideoFormat,
	 * corresponding to the format specified in sdpFormatStr
	 * 
	 * @param sdpEncodingStr
	 *            the SDP index that we'd like to convert to a JMF format.
	 * @return one of the AudioFormat.XXX or VideoFormat.XXX format strings.
	 */
	public static String sdpToJmfEncoding(String sdpEncodingStr) {
		int sdpEncoding = UNKNOWN_ENCODING;

		try {
			sdpEncoding = Integer.parseInt(sdpEncodingStr);
		} catch (NumberFormatException ex) {
			return null;
		}

		switch (sdpEncoding) {
		case SdpConstants.PCMU:
			return AudioFormat.ULAW_RTP;

		case SdpConstants.GSM:
			return AudioFormat.GSM_RTP;

		case SdpConstants.G723:
			return AudioFormat.G723_RTP;

		case SdpConstants.DVI4_8000:
			return AudioFormat.DVI_RTP;

		case SdpConstants.DVI4_16000:
			return AudioFormat.DVI_RTP;

		case SdpConstants.PCMA:
			return AudioFormat.ALAW;

		case SdpConstants.G728:
			return AudioFormat.G728_RTP;

		case SdpConstants.G729:
			return AudioFormat.G729_RTP;

		case SdpConstants.H263:
			return VideoFormat.H263_RTP;

		case SdpConstants.JPEG:
			return VideoFormat.JPEG_RTP;

		case SdpConstants.H261:
			return VideoFormat.H261_RTP;
		case 99:
			return "mpegaudio/rtp, 48000.0 hz, 16-bit, mono";
		default:
			return null;
		}
	}

	/**
	 * Returns the SDP encoding number corresponding to <tt>jmfFormat</tt>.
	 * 
	 * @param jmfEncoding
	 *            one of the AudioFormat.XXX or VideoFormat.XXX format strings.
	 * 
	 * @return the SDP index corresponding to <tt>jmfEncoding</tt>. Returns
	 *         {@link #UNKNOWN_ENCODING} if the encoding is not supported or
	 *         <code>jmfEncoding</code> is <code>null</code>.
	 */
	public static int jmfToSdpEncoding(String jmfEncoding) {
		if (jmfEncoding == null) {
			return UNKNOWN_ENCODING;
		} else if (jmfEncoding.equals(AudioFormat.ULAW_RTP)) {
			return SdpConstants.PCMU;
		} else if (jmfEncoding.equals(AudioFormat.GSM_RTP)) {
			return SdpConstants.GSM;
		} else if (jmfEncoding.equals(AudioFormat.G723_RTP)) {
			return SdpConstants.G723;
		} else if (jmfEncoding.equals(AudioFormat.DVI_RTP)) {
			return SdpConstants.DVI4_8000;
		} else if (jmfEncoding.equals(AudioFormat.DVI_RTP)) {
			return SdpConstants.DVI4_16000;
		} else if (jmfEncoding.equals(AudioFormat.ALAW)) {
			return SdpConstants.PCMA;
		} else if (jmfEncoding.equals(AudioFormat.G728_RTP)) {
			return SdpConstants.G728;
		} else if (jmfEncoding.equals(AudioFormat.G729_RTP)) {
			return SdpConstants.G729;
		} else if (jmfEncoding.equals(VideoFormat.H263_RTP)) {
			return SdpConstants.H263;
		} else if (jmfEncoding.equals(VideoFormat.JPEG_RTP)) {
			return SdpConstants.JPEG;
		} else if (jmfEncoding.equals(VideoFormat.H261_RTP)) {
			return SdpConstants.H261;
		} else {
			return UNKNOWN_ENCODING;
		}
	}

	/**
	 * Converts the list of <tt>sdpEncodings</tt> to a list of jmf compatible
	 * encoding strings as specified by the static vars in VideoFormat and
	 * AudioFormat.
	 * 
	 * @param sdpEncodings
	 *            a list containing strings representing SDP format codes.
	 * @return a list of strings representing JMF compatible encoding names.
	 */
	public static List<String> sdpToJmfEncodings(List<String> sdpEncodings) {
		List<String> jmfEncodings = new ArrayList<String>();

		if (sdpEncodings != null) {
			for (String sdpEncoding : sdpEncodings) {
				String jmfEncoding = sdpToJmfEncoding(sdpEncoding);

				if (jmfEncoding != null) {
					jmfEncodings.add(jmfEncoding);
				}
			}
		}
		return jmfEncodings;
	}
}
